package top.wilsonlv.jaguar.cloud.upms.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;
import top.wilsonlv.jaguar.basecrud.BaseModel;
import top.wilsonlv.jaguar.basecrud.util.LongUtil;
import top.wilsonlv.jaguar.cloud.upms.entity.Dept;
import top.wilsonlv.jaguar.cloud.upms.entity.User;
import top.wilsonlv.jaguar.cloud.upms.sdk.vo.DeptVO;
import top.wilsonlv.jaguar.cloud.upms.sdk.vo.RoleVO;
import top.wilsonlv.jaguar.cloud.upms.sdk.vo.UserVO;
import top.wilsonlv.jaguar.cloud.upms.service.DeptService;
import top.wilsonlv.jaguar.cloud.upms.service.RoleMenuService;
import top.wilsonlv.jaguar.cloud.upms.service.UserService;
import top.wilsonlv.jaguar.commons.data.encryption.util.EncryptionUtil;
import top.wilsonlv.jaguar.commons.mybatisplus.extension.JaguarLambdaQueryWrapper;
import top.wilsonlv.jaguar.commons.web.response.JsonResult;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author lvws
 * @since 2022/8/3
 */
@RestController
@RequestMapping("/common")
@RequiredArgsConstructor
@Api(tags = "公共API")
public class CommonApi {

    private final UserService userService;

    private final DeptService deptService;

    private final RoleMenuService roleMenuService;

    @ApiOperation(value = "获取随机码")
    @GetMapping("/randomCode")
    public JsonResult<String> randomCode(@RequestParam(required = false, defaultValue = "2") Integer upperCaseLetterLength,
                                         @RequestParam(required = false, defaultValue = "2") Integer lowerCaseLetterLength,
                                         @RequestParam(required = false, defaultValue = "4") Integer numLength) {
        String randomPassword = EncryptionUtil.randomCode(upperCaseLetterLength, lowerCaseLetterLength, numLength);
        return JsonResult.success(randomPassword);
    }

    @ApiOperation(value = "分页查询用户的简单信息")
    @GetMapping(value = "/user/page")
    public JsonResult<Page<UserVO>> userPage(
            @ApiIgnore Page<User> page,
            @ApiParam(value = "模糊用户信息") @RequestParam(required = false) String fuzzyUserInfo,
            @ApiParam(value = "用户部门ID集合") @RequestParam(required = false) Long[] userDeptIds) {

        LambdaQueryWrapper<User> wrapper = JaguarLambdaQueryWrapper.<User>newInstance()
                .eq(User::getUserEnable, true)
                .select(BaseModel::getId, User::getUserAccount, User::getUserNickName,
                        User::getUserPhone, User::getUserEmail);
        if (StringUtils.hasText(fuzzyUserInfo)) {
            wrapper.and(w -> w.like(User::getUserAccount, fuzzyUserInfo).or()
                    .like(User::getUserPhone, fuzzyUserInfo).or()
                    .like(User::getUserEmail, fuzzyUserInfo));
        }
        if (userDeptIds != null && userDeptIds.length > 0) {
            if (userDeptIds.length == 1) {
                wrapper.eq(User::getUserDeptId, userDeptIds[0]);
            } else {
                wrapper.in(User::getUserDeptId, Arrays.asList(userDeptIds));
            }
        }

        page = userService.query(page, wrapper);
        return JsonResult.success(userService.toVoPage(page, UserVO.class));
    }

    @ApiOperation(value = "根据父ID查询部门树")
    @GetMapping(value = "/dept/tree")
    public JsonResult<List<DeptVO>> deptTree(@RequestParam(required = false, defaultValue = "0") Long parentId) {
        if (LongUtil.isNull(parentId)) {
            return JsonResult.success(deptService.treeChildren(0));
        }

        Dept dept = deptService.getById(parentId);
        DeptVO deptVO = dept.toVo(DeptVO.class);
        deptVO.setChildren(deptService.treeChildren(parentId));
        return JsonResult.success(Collections.singletonList(deptVO));
    }

    @ApiOperation(value = "查询当前用户角色子集")
    @GetMapping(value = "/role/list")
    public JsonResult<List<RoleVO>> roleList(
            @ApiParam(value = "模糊角色名称") @RequestParam(required = false) String fuzzyRoleName) {
        return JsonResult.success(roleMenuService.listGrantedRoles(fuzzyRoleName));
    }

}
